package com.hy.study.controller;

import com.hy.study.config.JdbcConfig;
import com.hy.study.config.PersonConfig;
import com.hy.study.dto.Task;
import com.hy.study.dto.User;
import com.hy.study.retry.ThirdPartyService;
import com.hy.study.service.TaskService;
import com.hy.study.service.UserService;
import com.hy.study.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author huyong
 * @date 2024 年 01 月 05 日 16:31: 59
 */
@RestController
@Slf4j
public class TestController {
    @Autowired
    private UserService userService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private JdbcConfig jdbcConfig;
    @Autowired
    private PersonConfig personConfig;
    @Autowired
    private ThirdPartyService thirdPartyService;

    @GetMapping(path = "/getUserList")
    public Result<List<User>> getUserList() {
        return new Result<>(userService.list());
    }

    @RequestMapping("/zeroException")
    public int zeroException() {
        return 100 / 0;
    }


    @ResponseBody
    @GetMapping("/saveTask/{id}")
    public Task saveTask(@PathVariable("id") long id) {
        Task task = new Task();
        task.setCreationDate(new Date());
        task.setTaskName("保存" + id);
        task.setId(id);
        return taskService.save(task);
    }

    @GetMapping("jdbcConfig")
    public JdbcConfig getJdbcConfig() {
        return this.jdbcConfig;
    }

    @GetMapping("personConfig")
    public PersonConfig getPersonConfig() {
        return this.personConfig;
    }


    @GetMapping("/test")
    @ResponseBody
    public Result<String> test() {
        Task loaded = taskService.findById(1L);
        log.info("loaded-->{}", loaded);
        Task cached = taskService.findById(1L);
        log.info("cached-->{}", cached);
        loaded = taskService.findById(2L);
        log.info("loaded2-->{}", loaded);
        return new Result<>("操作成功");
    }

    @GetMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") long id) {
        taskService.deleteFromCache(id);
        return new Result<>("删除成功");
    }

    @GetMapping("/test1")
    public Result<String> test1() {
        taskService.test();
        log.info("test1--");
        return new Result<>("OK");
    }

    @ResponseBody
    @GetMapping("getUserById/{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id) {
        return new Result<>(userService.getUserInfoByUserId(id));
    }

    @ResponseBody
    @GetMapping("updateUser/{id}")
    public Result<String> updateUser(@PathVariable("id") Integer id) {
        User user = userService.getUserInfoByUserId(id);
        user.setUsername("更新112");
        userService.updateUser(user);
        return new Result<>();
    }

    @ResponseBody
    @GetMapping("reload")
    public Result<String> reload() {
        userService.reload();
        return new Result<>();
    }

    @ResponseBody
    @GetMapping("retry")
    public Result<String> retry() {
        thirdPartyService.callCsb();
        return new Result<>();
    }
}
